//引入组件库
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var listdataCommunity = mm("do_ListData");
var helperOpen = require("helper/open");
var bdmapHelper = require("helper/bdmap");
var communityUtil = require("helper/community");
//本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.distancePage4MallShop;

//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

// 声明UI变量
var do_ALayout_root = ui("do_ALayout_root");
var headerAout = ui("headerAout");
var listViewCommunity = ui("listViewCommunity");
var labelDingweiCity = ui("labelDingweiCity");
var textFieldSearch = ui("textFieldSearch");
var aLayoutChangeCity = ui("aLayoutChangeCity");

listViewCommunity.bindItems(listdataCommunity);

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "定位小区";
// 左上角-返回
ui(addheader + ".headerLeft").on("touch", function() {
	do_App.closePage()
});
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.text = '确定';
labelNext.visible=true;
headerRight.on("touch", function() {
	var communityIds = [];
	var communityNames = [];
	for(var i=0;i<listdataCommunity.getCount();i++){
		oneData = listdataCommunity.getOne(i);
		if(oneData.checked){
			communityIds.push(oneData.id);
			communityNames.push(oneData.name);
		}
	}
	
	do_App.fire("fireShopUpdate",{"extCommunityIds":communityIds.join(';'),"extCommunityNames":communityNames.join(',')});
	do_App.closePage();
})

var page = 0;
var dingweiX, dingweiY, dingweiCity;
var extCommunityIds = '';
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	extCommunityIds = do_Page.getData().extCommunityIds;//已选的社区ids
	dingweiX = do_Page.getData().x;
	dingweiY = do_Page.getData().y;
	deviceone.print(dingweiX+"---"+dingweiY);
	if(dingweiX==null || dingweiY==null){
		dingweiX=bdmapHelper.getDingwei().longitude;
		dingweiY=bdmapHelper.getDingwei().latitude;
	}
	dingweiCity=bdmapHelper.getDingwei().cityName;
	if (null != dingweiCity && dingweiCity.trim() != '') {
		labelDingweiCity.text = dingweiCity;
	} else {
		labelDingweiCity.text = "定位失败";
	}
	loadPage();
});
var search;
textFieldSearch.on("enter", function(data, e) {
	search = this.text;
	loadPage();
});
aLayoutChangeCity.on("touch", function() {
	helperOpen.open("source://view/area/cityMain.ui");
});
function loadPage() {
	page = 1;
	var params = {
		page : page,
		x : dingweiX,
		y : dingweiY,
		cityName : dingweiCity,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listViewCommunity.rebound();
		listdataCommunity.removeAll();
		listdataCommunity.addData(data.page.content);
		listViewCommunity.refreshItems();
		checkOldCommunity();
		if (data.page.content.length <= 0) {// 没有社区
			listdataCommunity.addOne({
				template : 1
			}, 0);
			listViewCommunity.refreshItems();
		}
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		x : dingweiX,
		y : dingweiY,
		cityName : dingweiCity,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listViewCommunity.rebound();
		listdataCommunity.addData(data.page.content);
		listViewCommunity.refreshItems();
		checkOldCommunity();
	})
}
// 下拉列表，刷新数据
listViewCommunity.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listViewCommunity.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});

// 从选择城市页面进入
do_Page.on("result", function(data) {
	dingweiCity = data.cityName;
	labelDingweiCity.text = dingweiCity;
	search = "";
	textFieldSearch.text = "";
	loadPage();
});

//更改选中状态
do_Page.on("listCheckChange", function(obj) {
	var oneData = null;
	for(var i=0;i<listdataCommunity.getCount();i++){
		oneData = listdataCommunity.getOne(i);
		if(oneData.id == obj.id){
			oneData.checked = obj.checked;
			listdataCommunity.updateOne(i, oneData);//全部设为“不选中”或"选中"
		}
	}
})

//选中现在已经服务的社区
function checkOldCommunity(){
	if(extCommunityIds!=null && extCommunityIds!=""){
		var oneData = null;
		for(var i=0;i<listdataCommunity.getCount();i++){
			oneData = listdataCommunity.getOne(i);
			if(extCommunityIds.indexOf(oneData.id)>-1){
				oneData.checked = true;
				listdataCommunity.updateOne(i, oneData);//全部设为“不选中”或"选中"
			}
		}
		listViewCommunity.refreshItems();
	}
}